<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Auth\ThrottlesLogins;


class LoginController extends Controller
{
    use ThrottlesLogins;

    //登录页面
    public function index()
    {
        dd('123');
        //return view('login/login');
    }

    //修改页面
    public function member_edit()
    {
        $member = DB::table('admin')->where('id', '=', Session::get('session_id'))->first();

        return view('member/member', ['account' => $member->account]);
    }

    //修改登录密码
    public function login_edit_pwd(Request $request)
    {
        $input = $request->input();
        if (empty($input['password'])) return self::fail('密码为空');

        DB::table('admin')->where('id', '=', Session::get('session_id'))->update(['pwd' => Hash::make($input['password'])]);

        return self::success();
    }

    //登录认证
    public function login_do(Request $request)
    {

        if ($this->hasTooManyLoginAttempts($request)) {
            return $this->sendLockoutResponse($request);
        }

        $input = $request->input();

        // 登录验证
        $result = DB::table('admin_users')->where('account', '=', $input['username'])->first();

        if (!isset($result->id)) self::fail('"密码错误或者账号错误!"');
        if (!Hash::check($input['password'], $result->password)) self::fail('"密码错误或者账号错误!"');

        return self::success();

    }

    //退出登录
    public function login_up()
    {

        \Session::forget('session_id');

        return redirect('/login');

    }
}
